(function() {
	// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
	// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
	// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
	// MIT license

    var lastTime = 0;
    var vendors = ['ms', 'moz', 'webkit', 'o'];
    for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
        window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
        window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] 
                                   || window[vendors[x]+'CancelRequestAnimationFrame'];
    }
 
    if (!window.requestAnimationFrame)
        window.requestAnimationFrame = function(callback, element) {
            var currTime = new Date().getTime();
            var timeToCall = Math.max(0, 16 - (currTime - lastTime));
            var id = window.setTimeout(function() { callback(currTime + timeToCall); }, 
              timeToCall);
            lastTime = currTime + timeToCall;
            return id;
        };
 
    if (!window.cancelAnimationFrame)
        window.cancelAnimationFrame = function(id) {
            clearTimeout(id);
        };
}());
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 600;
document.body.appendChild(canvas);
			

// Background image
var bgReady1 = false;
var bgImage1 = new Image();
bgImage1.onload = function() {
	bgReady1 = true;
};
bgImage1.src = "images/cat-bg-sai3.png";

// cat image
var catReady = false;
var catImage = new Image();
catImage.onload = function() {
	catReady = true;
};
catImage.src = "images/cat-and-p-resize.png";

// human image
var humanReady = false;
var humanImage = new Image();
humanImage.onload = function() {
	humanReady = true;
};
humanImage.src = "images/human.png";

var human2Ready = false;
var human2Image = new Image();
human2Image.onload = function() {
	human2Ready = true;
};
human2Image.src = "images/human.png";

// Chomp sound
var snd = new Audio("audio/Cat.mp3");
// Under human image

// Game objects
var cat = {
	speed : 100, // movement in pixels per second
	x : 0,
	y : 460
};

var human = {
	
	speed : 400,
	x : 0,
	y : 0
};

var human2 = {
	
	speed : 400,
	x : 201,
	y : 0
};

// Handle keyboard controls
var keysDown = {};
addEventListener("keydown", function(e) {
	keysDown[e.keyCode] = true;
}, false);
addEventListener("keyup", function(e) {
	delete keysDown[e.keyCode];
}, false);
var humansCaught = -1;


var i,j;
var start = true;
// Reset the game when the player catches a human
var reset = function() {
	if (start) {
		cat.x = 334;
		//cat.y = canvas.height / 2;
		start = false;
	}
	// Throw the human somewhere on the screen randomly
		human.y = 0;
		do { 
			i = Math.floor(Math.random()*(900));
		
			//alert(i);
			if(i<=300){
				human.x = 201;
			}
			else if(i>300&&i<=600){
				human.x = 334;
			}
			else{
				human.x = 462;
			}
		} while(human.x<=200 || human.x>=463);// Can't born at tree
		
		var Time = setTimeout("setHuman2();",20000);
	

	++humansCaught;
	win();

};



var gameover = function(){
	
	alert("Game Over");
	humansCaught = 0;
	
};
var setHuman2 = function(){
	human2.y = 0;
		do { 
			j = Math.floor(Math.random()*(900));
			//alert(i);
				if(j<=300){
					human2.x = 201;
				}
				else if(j>300&&i<=600){
				human2.x = 334;
				}
				else{
					human2.x = 462;
				}
			
		} while(human2.x<=200 || human2.x>=463);
	++humansCaught;
	win();
};

var goHuman2 = function(){
	if(human2.y <500 )
	{
		human2.y = (human2.y + human2.speed * modifier) % canvas.height;
	}
	else
		setHuman2();	
};
var win = function(){
	if(humansCaught==21){
		alert("You Win passs word is 14235");
		window.close();
		
	}
};

var update = function(modifier) {
	
	// Player holding down
	if (human.y<500) // Can't pass the tree
	{
			human.y = (human.y + human.speed * modifier) % canvas.height;	
	}	
	else 
		reset();
	
	if(human2.y <500 )
	{
		human2.y = (human2.y + human2.speed * modifier) % canvas.height;
	}
	else
		setHuman2();	
	
		
	
	if (40 in keysDown) { // Player holding down
			cat.x = 334;
	}
	
	if (37 in keysDown) {// Player holding left
			cat.x = 201;
	}
	if (39 in keysDown) {// Player holding right
			cat.x = 462;		
	}

	if(cat.x==human.x&&(human.y>=cat.y)||cat.x==human2.x&&(human2.y>=cat.y)){
		if (snd.currentTime > 0) {
		snd.currentTime = 0;
		//this is to make sure the sound resets if it is still playing
		}
		snd.play();
		gameover();
	}
	
	
	
};

// Draw everything
var render = function() {
	if (bgReady1) {
		ctx.drawImage(bgImage1, 0, 0);
	}
	if (catReady) {
		ctx.drawImage(catImage, cat.x, cat.y);
	}
	if (humanReady) {
		ctx.drawImage(humanImage, human.x,human.y);
	}
	if (human2Ready) {
		ctx.drawImage(human2Image, human2.x,human2.y);
	}
	// Score
	ctx.fillStyle = "rgb(255, 255, 0)";
	ctx.font = "24px Helvetica";
	ctx.textAlign = "left";
	ctx.textBaseline = "top";
	ctx.fillText("" + humansCaught, 90, 120);
};

// The main game loop
var main = function() {
	var now = Date.now();
	var delta = now - then;
	update(delta / 1000);
	render();
	
	then = now;
};

// Let's play this game!0
reset();
var then = Date.now();
setInterval(main, 1);
// Execute as fast as possible
